package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.DateConvert;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@SuppressWarnings("all")
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;


    //获取物资列表 带条件查询 带分页
    @Override
    public PageVO<ProductVO> findProductList(int pageNum, int pageSize, String name, Integer categoryId, String supplier, Integer status, Integer[] categorys) {
        PageHelper.startPage(pageNum, pageSize);//只对该语句以后的第一个查询语句得到的数据进行分页
        List<Product> productList = null;
        if (null != categorys) {
            int length = categorys.length;
            productList = productMapper.findProductList(name, categoryId, supplier, status, categorys, length);
        } else {
            productList = productMapper.findProductList(name, categoryId, supplier, status, null, 0);
        }
        List<ProductVO> productVOList = productList.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
        PageInfo<Product> ProductVOPageInfo = new PageInfo<>(productList);
        return new PageVO<ProductVO>(ProductVOPageInfo.getTotal(), productVOList);
    }

    //添加物资
    @Override
    public void add(Map addRuleForm) {
        Product product = new Product();
        try {
            org.apache.commons.beanutils.BeanUtils.populate(product, addRuleForm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        product.setPNum(UUID.randomUUID().toString());
        List categoryKeys = (List) addRuleForm.get("categoryKeys");
        product.setOneCategoryId(Long.valueOf(String.valueOf(categoryKeys.get(0))));
        product.setTwoCategoryId(Long.valueOf(String.valueOf(categoryKeys.get(1))));
        product.setThreeCategoryId(Long.valueOf(String.valueOf(categoryKeys.get(2))));
        product.setStatus(2);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        productMapper.insert(product);
    }

    //修改物资时回显物资信息
    @Override
    public Product selectProductById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    //修改物资信息
    @Override
    public void update(Long id, Map editRuleForm) {
        System.out.println(editRuleForm.get(""));
        Product product = new Product();
        DateConvert.Convert();//其实根本没有日期信息 只不过转换有冲突
        try {
            org.apache.commons.beanutils.BeanUtils.populate(product, editRuleForm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        List categoryKeys = (List) editRuleForm.get("categoryKeys");
        product.setOneCategoryId(Long.valueOf(String.valueOf(categoryKeys.get(0))));
        product.setTwoCategoryId(Long.valueOf(String.valueOf(categoryKeys.get(1))));
        product.setThreeCategoryId(Long.valueOf(String.valueOf(categoryKeys.get(2))));
        product.setModifiedTime(new Date());
        productMapper.updateByPrimaryKeySelective(product);
    }

    //移除物资信息到回收站
    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        productMapper.updateByPrimaryKeySelective(product);
    }

    //从回收站恢复物资信息
    @Override
    public void back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product);
    }

    //审核通过物资  直接调用back显得语义不明
    @Override
    public void publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product);
    }

    //删除物资来源信息
    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    //获取所有物资信息
    @Override
    public PageVO<ProductVO> findProducts(int pageNum, int pageSize, Integer status) {
        List<Product> productList = productMapper.findProducts(status);
        List<ProductVO> productVOList = productList.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
        List<ProductVO> productVOListPage = ListPageUtils.page(productVOList, pageSize, pageNum);
        //注意这里是 supplierVOList 的长度 !!!
        return new PageVO<ProductVO>(productVOList.size(), productVOListPage);
    }
}
